/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024.
 * Description: support memory snapshot feature
 * Author: heyuqiang
 * Create: 2024-07-01
 */
#ifndef RTOS_MEM_SNAPSHOT_H
#define RTOS_MEM_SNAPSHOT_H

#ifdef CONFIG_RTOS_MEM_SNAPSHOT
#include <linux/cma.h>

#define CMA_BLOCK_VALID 1
#define CMA_BLOCK_INVALID 0
#define CMA_HEADER_PFN_START (PHYS_PFN(cma->bitmap_size + PAGE_SIZE))

/* Data structure stored in the persistent memory. The data is not lost after restart. */
struct persist_mem_block {
	int id;
	bool reset_on_reboot;
	int block_valid;
	unsigned int crc32_bitmap;
	unsigned int crc32_page;
};

struct mem_snapshot_task {
	int cma_id;
	pid_t pid;
	struct vm_area_struct *vma;
	unsigned long vm_start;
	unsigned long vm_end;
	unsigned long *phymap_bitmap;
	struct list_head list;
};

extern void __init rtos_cma_activate_area(struct cma *cma);
extern bool mem_snapshot_is_enabled(void);
extern int cma_alloc_from_pfn(struct cma *cma, unsigned long pfn, size_t count, unsigned int align,
	bool no_warn);
extern struct page *rtos_cma_alloc(struct cma *cma, size_t count, unsigned int align, bool no_warn);
extern bool rtos_cma_release(struct cma *cma, const struct page *pages, unsigned int count);
extern int cma_release_from_count(struct cma *cma, const struct page *pages, unsigned int count);
extern int get_cma_page_refcount(int cma_id, unsigned long offset);
extern void rtos_init_cma_reserved_pageblock(struct page *page, unsigned long *bitmap);
extern int add_new_alloc(struct list_head *head, unsigned long pfn_start, unsigned long count);
extern void failed_release_new_alloc(struct cma *cma, struct list_head *head);
extern void success_release_list(struct list_head *head);
extern bool bitmap_get_bitvalue(unsigned long *bitmap, unsigned long offset);
#else
static inline bool mem_snapshot_is_enabled(void) { return false; }
static inline void rtos_cma_activate_area(struct cma *cma) {}
#endif /* CONFIG_RTOS_MEM_SNAPSHOT */

#endif
